/*
 * Copyright 2023 The Bazel Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.idea.blaze.qsync.cc;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.idea.blaze.common.Label;
import com.google.idea.blaze.qsync.deps.ArtifactTracker;
import com.google.idea.blaze.qsync.deps.CcCompilationInfo;
import com.google.idea.blaze.qsync.deps.CcToolchain;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;

/**
 * Class encapsulating the info produced by the aspect when building dependencies for cc targets.
 */
@AutoValue
public abstract class CcDependenciesInfo {
  public static final CcDependenciesInfo EMPTY = create(ImmutableMap.of(), ImmutableMap.of());

  public abstract ImmutableMap<Label, CcCompilationInfo> targetInfoMap();

  public abstract ImmutableMap<String, CcToolchain> toolchainInfoMap();

  public static CcDependenciesInfo create(ArtifactTracker.State artifactState) {
    return create(
        artifactState.targets().stream()
            .filter(e -> e.ccInfo().isPresent())
            .map(e -> new SimpleEntry<>(e.label(), e.ccInfo().get()))
            .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)),
        artifactState.ccToolchainMap());
  }

  public static CcDependenciesInfo create(
      ImmutableMap<Label, CcCompilationInfo> targetInfoMap,
      ImmutableMap<String, CcToolchain> toolchainInfoMap) {
    return new AutoValue_CcDependenciesInfo(targetInfoMap, toolchainInfoMap);
  }

}
